package com.tj.shopping.service.impl;

import com.tj.shopping.entity.Order;
import com.tj.shopping.mapper.GoodsMapper;
import com.tj.shopping.mapper.OrderMapper;
import com.tj.shopping.service.OrderService;
import com.tj.shopping.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderServiceImpl implements OrderService {

    private OrderMapper mapper;
    @Override
    public int remove(Map<String, Object> map) {
        SqlSession session= MybatisUtil.getSession(true);
        mapper=session.getMapper(OrderMapper.class);
        int result= mapper.remove(map);
        MybatisUtil.close(session);
        return result;
    }
    @Override
    public Map<String, Object> findall() {
        SqlSession session = MybatisUtil.getSession(true);
        mapper = session.getMapper(OrderMapper.class);
        List<Map<String,Object>> list1 = mapper.findall();

        for(int i=0; i<list1.size();i++) {
            String paytime=list1.get(i).get("paytime").toString();
            paytime = paytime.replace("T", " ");
            list1.get(i).put("paytime", paytime);
        }

        Map<String,Object> map2 = new HashMap<>();
        if (null == list1 || list1.isEmpty()){
            map2.put("code",600);
            map2.put("msg","暂无数据");
        } else {
            map2.put("code",200);
            map2.put("msg","成功");
            map2.put("data",list1);
        }
        MybatisUtil.close(session);
        return map2;
    }

    @Override
    public Map<String, Object> finds(Map<String, Object> map) {
        SqlSession session= MybatisUtil.getSession(true);
        mapper=session.getMapper(OrderMapper.class);
        List<Map<String,Object>> orders=mapper.finds(map);
        System.out.println(orders);
        Map<String,Object>map1=new HashMap<>();

        if(orders==null||orders.isEmpty()){
            map1.put("code",600);
            map1.put("msg","暂无数据");


        }else {
            map1.put("code",200);
            map1.put("msg","成功");
        }
        map1.put("data",orders);
        MybatisUtil.close(session);

        return map1;
    }

    @Override
    public Map<String, Object> findReturn(Map<String, Object> map) {
        SqlSession session= MybatisUtil.getSession(true);
        mapper=session.getMapper(OrderMapper.class);
        List<Map<String,Object>> orders=mapper.findReturn(map);
        System.out.println(orders);
        Map<String,Object>map1=new HashMap<>();

        if(orders==null||orders.isEmpty()){
            map1.put("code",600);
            map1.put("msg","暂无数据");

        }else {
            map1.put("code",200);
            map1.put("msg","成功");
        }
        map1.put("data",orders);
        MybatisUtil.close(session);
        return map1;

    }

    @Override
    public int update(Order order) {
        SqlSession session = MybatisUtil.getSession(true);
        mapper = session.getMapper(OrderMapper.class);
        return mapper.update(order);
    }

    @Override
    public int add(Order order) {
        SqlSession session = MybatisUtil.getSession(true);
        mapper = session.getMapper(OrderMapper.class);
        return mapper.add(order);
    }

    @Override
    public int selectOno() {
        SqlSession session = MybatisUtil.getSession(true);
        mapper = session.getMapper(OrderMapper.class);
        return mapper.selectOno();
    }


    @Override
    public Map<String, Object> find() {
        SqlSession session = MybatisUtil.getSession(true);
        mapper = session.getMapper(OrderMapper.class);
        List<Map<String,Object>> list = mapper.find();

        Map<String,Object> responseData = new HashMap<>();
        if (null == list || list.isEmpty()){
            responseData.put("code",600);
            responseData.put("msg","暂无数据");
        } else {
            responseData.put("code",200);
            responseData.put("msg","成功");

            // 将查询到的数据按照 ECharts 饼图需要的格式进行封装
            List<Map<String, Object>> formattedData = new ArrayList<>();
            for (Map<String, Object> item : list) {
                Map<String, Object> dataItem = new HashMap<>();
                dataItem.put("value", item.get("deNum"));
                dataItem.put("name", item.get("tname"));
                formattedData.add(dataItem);
            }
            responseData.put("data", formattedData);
        }
        MybatisUtil.close(session);
        return responseData;
    }
}
